#include "llvm/Target/TargetMachine.h"
#include "llvm/Support/Error.h"

static Expected<std::unique_ptr<TargetMachine>> createTargetMachine(const Triple& Triple) {
    std::string Error;
    const Target* TheTarget = TargetRegistry::lookupTarget(Triple.str(), Error);
    if (!TheTarget)
        return createStringError(inconvertibleErrorCode(), "Target lookup failed: %s", Error.c_str());

    TargetOptions Options;
    Optional<Reloc::Model> RM;
    auto TargetMachine = TheTarget->createTargetMachine(Triple.str(), "generic", "", Options, RM);
    if (!TargetMachine)
        return createStringError(inconvertibleErrorCode(), "Failed to create target machine");

    return std::unique_ptr<TargetMachine>(TargetMachine);
}

auto TargetMachineOrErr = createTargetMachine(ES->getExecutorProcessControl().getTargetTriple());
if (!TargetMachineOrErr)
return TargetMachineOrErr.takeError();

auto DL = (*TargetMachineOrErr)->createDataLayout();
if (!DL)
return DL.takeError();
